const {
  friendshipAssociationDetail,
  friendshipAssociationMemberList,
  friendshipAssociationActivityList,
  friendshipAssociationCollect,
  friendshipAssociationCancelCollect
} = require('../../../../api/fraternity');

const {
  sliceString,
  formatTime2,
  isFinished,
  joinBase64
} = require('../../../../utils/util');
 
const {
  getBase64
} = require('../../../../api/index');
const {
  getDict
} = require('../../../../utils/dict');

const { home1 } = require('../../../utils/base64/home1');
// pages/fraternity/detail/index.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    home1,
    id: '',
    finished: false,
    detail: {},
    activePage: {
      pageNumber: 1,
      pageSize: 20,
      friendshipAssociationId: ''
    },
    memberPage: {
      pageNumber: 1,
      pageSize: 7,
      orderBy: "CREATE_TIME DESC",
      friendshipAssociationId: ''
    },
    memberTotal: 0,
    activeList: [], //  活动列表
    memberList: [] //  成员列表
  },
  loadMore() { //  加载更多
    fetchFriendshipAssociationActivityList.call(this, id);
  },
  onClick(event) {
    const {
      url
    } = event.currentTarget.dataset;
    wx.navigateTo({
      url
    });
  },
  onBack() {
    wx.navigateBack();
  },
  onRefresh() {
    this.data.finished = false;
    this.data.activePage.pageNumber = 1;
    this.setData({ activeList: [] }, () => {
      fetchFriendshipAssociationActivityList.call(this);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.setData({
      id: options.id
    }, () => {
      fetchFriendshipAssociationDetail.call(this);
      fetchFriendshipAssociationMemberList.call(this);
      fetchFriendshipAssociationActivityList.call(this);
    });
  }
})

//  获取 联谊会 详情
async function fetchFriendshipAssociationDetail() {
  const {
    errors = [],
      friendshipAssociation
  } = await friendshipAssociationDetail({
    id: this.data.id
  });
  if (errors.length > 0) {
    return;
  }

  this.setData({
    detail: friendshipAssociation
  });
}

//  获取 联谊会 成员列表
async function fetchFriendshipAssociationMemberList() {
  this.data.memberPage.friendshipAssociationId = this.data.id;

  const {
    errors = [], result, totalCount
  } = await friendshipAssociationMemberList(this.data.memberPage);
  if (errors.length > 0) {
    return;
  }

  const _data = await setResult3(result.slice(0, 7));

  this.setData({
    memberTotal: totalCount
  });
  this.setData({
    memberList: _data
  });
}

//  加工数据
async function setResult3(data) {
  const _data = JSON.parse(JSON.stringify(data));

  await Promise.all(_data.map(async _s => {
    const avatar = joinBase64(await getBase64(_s.avatar));
    _s.avatar = avatar;
    return _s;
  }))

  return _data;
}


//  获取 联谊会 活动列表
async function fetchFriendshipAssociationActivityList() {
  this.data.activePage.friendshipAssociationId = this.data.id;
  wx.showLoading({
    title: '加载中...'
  });
  const {
    errors,
    result,
    totalCount
  } = await friendshipAssociationActivityList(this.data.activePage);
  if (errors.lenght > 0) {
    return;
  }

  //  判断数据是不是都已经加载完了
  this.setData({
    finished: isFinished(totalCount, this.data.activePage.pageNumber * this.data.activePage.pageSize)
  });
  //  加载更多数据
  this.setData({
    activeList: this.data.activeList.concat(await setResult2(result))
  });
  this.data.activePage.pageNumber++;

  wx.hideLoading();
}

//  处理 联谊会 活动列表 数据
async function setResult2(data = []) {
  const dict = await getDict('ACTIVITY_STATE');
  console.log(dict);

  const _data = JSON.parse(JSON.stringify(data));

  _data.map(item => {
    item.activityBeginTime = formatTime2(item.activityBeginTime);
    item.activityEndTime = formatTime2(item.activityEndTime);
    item.registrationBeginTime = formatTime2(item.registrationBeginTime);
    item.registrationEndTime = formatTime2(item.registrationEndTime);
    item.status = 'status-' + item.activityState;
    item.statusName = dict.find(_s => _s.code === item.activityState)?.name;
    return item;
  })

  return _data;
}
